package pais.www.stat;

import java.io.PrintWriter;
import java.util.StringTokenizer;

import pais.db.Keyword;
import pais.db.KeywordTable;
import papos.rowset.KeyNameNotFoundException;
import papos.rowset.StorageSystem;
import pasys.error.*;


class PrintAllKeys {
  public static final int KEYWORD_COUNT_PER_LINE = 10;

  public static void printKeys(PrintWriter toClient, StringTokenizer stok) throws PaposException  {
    int skipCount = 0;
    if (stok.hasMoreTokens()) {
      try {
        skipCount = Integer.parseInt(stok.nextToken());
        toClient.println("printing all keywords skiping " + skipCount
            + " keywords.");
        printAllKeywords(toClient, skipCount);
      } catch (NumberFormatException e) {
        Commands.printStatUsage(toClient);
      }
    } else {
      toClient.println("printing all keywords.");
      printAllKeywords(toClient, 0);
    }
  }

  public static void printAllKeywords(PrintWriter toClient, int skipCount) throws PaposException {
    long count = 0;
    KeywordTable keywordTable = KeywordTable.getInstance();
    Keyword kw = new Keyword();
    StorageSystem storage = keywordTable.getStorage();
    try {
      if (storage.findFirst("keyword_", kw)) {
        do {
          if (count > skipCount)
          {
            toClient.print("&lt;" + kw.getKeyword() + "&gt;");
            if ( (count % KEYWORD_COUNT_PER_LINE) == 0 ) 
              toClient.println("<br>");
          }
          count++;
        } while (storage.findNext("keyword_", kw));
      }
    } catch (KeyNameNotFoundException e) {
      assert(false);
    }
  }


}
